define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url : 'reservation/index' + location.search,
                    add_url   : 'reservation/add',
                    edit_url  : 'reservation/edit',
                    del_url   : 'reservation/del',
                    multi_url : 'reservation/multi',
                    import_url: 'reservation/import',
                    table     : 'reservation',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url     : $.fn.bootstrapTable.defaults.extend.index_url,
                pk      : 'id',
                sortName: 'id',
                columns : [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_type', title: __('病人类型'), formatter: Table.api.formatter.label, table: table, custom: {"老病人": "primary", "新病人": "info"}},
                        {field: 'disease_id_text', title: __('Disease_id')},
                        {field: 'stay_date', title: __('Stay_date'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false},
                        {field: 'stay_days', title: __('Stay_days'), operate: 'BETWEEN'},
                        {
                            field: 'is_allow_change_date', title: __('是否可调整日期'), operate: 'LIKE', formatter: function (value, row, index) {
                                switch (value) {
                                    case 0:
                                        return '<span style="color:red">否</span>';
                                    case 1:
                                        return '<span style="color:green">是</span>';
                                }
                            }
                        },
                        {field: 'patient_name', title: __('Patient_name'), operate: 'LIKE'},
                        {field: 'patient_cardno', title: __('Patient_cardno'), operate: 'LIKE'},
                        {field: 'accompany_name', title: __('Accompany_name'), operate: 'LIKE'},
                        {field: 'accompany_cardno', title: __('Accompany_cardno'), operate: 'LIKE'},
                        {field: 'accompany_mobile', title: __('Accompany_mobile'), operate: 'LIKE'},
                        {
                            field: 'status', title: __('Status'), formatter: function (value, row, index) {
                                switch (value) {
                                    case 0:
                                        return '待审核';
                                    case 1:
                                        return '<span style="color:green">审核通过</span>';
                                    case 2:
                                        return '<span style="color:red">已驳回</span>';
                                    case 3:
                                        return '<span style="color:#949494;">用户已撤销</span>';
                                    case 4:
                                        return '<span style="color:#949494;">用户已取消</span>';
                                }
                            }
                        },
                        {field: 'reject_reason', title: __('Reject_reason'), operate: 'LIKE'},
                        {field: 'memo', title: __('Memo'), operate: 'LIKE'},
                        {field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if ($.inArray(row.status, [2, 3, 4]) !== -1) {
                                    $(table).data("operate-edit", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ],

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add  : function () {
            Controller.api.bindevent();
        },
        edit : function () {
            $('#submitBth').click(function (e) {
                let curStatus = parseInt($('#c-status').val());
                let that = this;
                if (status === 1 && (curStatus === 2 || curStatus === 3)) {
                    Layer.confirm('已通过的预约修改为取消状态，预约将被无效，请谨慎操作！', {
                        title: "您确认取消此预约吗",
                        btn  : ['确认', '取消'] //按钮
                    }, function () {
                        $(that).closest("form").trigger("submit");
                        return true;
                    });
                    return false;
                }
                $(that).closest("form").trigger("submit");
                return false;
            });
            Controller.api.bindevent();
        },
        api  : {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});